package com.cx.bank.manager;

import com.cx.bank.dao.BankDaoImpl;
import com.cx.bank.model.MoneyBean;
import com.cx.bank.model.UserBean;
import com.cx.bank.util.AccountOverDrawnException;
import com.cx.bank.util.InvalidDepositException;
import com.sun.javafx.image.BytePixelSetter;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @author jxau_scf
 * @date 2021/7/13 - 20:39
 */
public class ManagerImpl implements IManager {
    //单例设计
    private static ManagerImpl managerInstance;

    private UserBean userBean;
    private BankDaoImpl bankDao = new BankDaoImpl();

    public UserBean getUserBean() {
        return userBean;
    }

    public void setUserBean(UserBean userBean) {
        this.userBean = userBean;
    }

    public BankDaoImpl getBankDao() {
        return bankDao;
    }

    public void setBankDao(BankDaoImpl bankDao) {
        this.bankDao = bankDao;
    }

    private ManagerImpl(){
    }
    public static synchronized ManagerImpl getInstance(){
        if (null != managerInstance){
            return managerInstance;
        }
        managerInstance = new ManagerImpl();
        return managerInstance;
    }



    /**
     *查询余额方法
     * @return
     */
    @Override
    public Double inquiry() {
        return userBean.getMoneyBean().getMoney();
    }



    /**
     * 取款方法（实现“余额不足”逻辑判断）
     */
    @Override
    public void withdrawals(Double money) throws InvalidDepositException {
//        double currentMoney = moneyBean.getMoney();
        double currentMoney = userBean.getMoneyBean().getMoney();
        if (currentMoney < money){
//            System.out.println("余额不足");
            throw new InvalidDepositException("余额不足");
        }
//        moneyBean.setMoney(currentMoney - money);
        userBean.getMoneyBean().setMoney(currentMoney - money);
    }

    /**
     * 存款方法（实现“存款不能为负”逻辑判断）
     */
    @Override
    public void deposit(double money) throws AccountOverDrawnException {
        if (money < 0){
//            System.out.println("存款不能为负");
            throw new AccountOverDrawnException("存款不能为负");
        }
//        double currentMoney = moneyBean.getMoney();
//        moneyBean.setMoney(currentMoney + money);
        Double currentMoney = userBean.getMoneyBean().getMoney();
        userBean.getMoneyBean().setMoney(currentMoney + money);
        System.out.println("存款成功");
    }

    /**
     * 退出系统方法
     */
    @Override
    public void exitSystem(UserBean userBean) throws IOException {
        bankDao.addBank(userBean.getUserName(), userBean.getMoneyBean());
    }

    /**
     * 注册功能
     * @param userBean
     * @throws IOException
     */
    public boolean register(UserBean userBean) throws IOException {
        UserBean userBeanInProperties = bankDao.findByName(userBean.getUserName());
        if (userBeanInProperties != null){
            System.out.println("用户已存在");
        }else {
            boolean status = bankDao.insertUser(userBean);
            if (status){
                System.out.println("注册成功！");
                return true;
            }else {
                System.out.println("注册失败！");
                return false;
            }
        }
        return false;

//        File file = new File("src/main/resources/"+userBean.getUserName()+".properties");
//        if (!file.exists()){
//            boolean status = bankDao.insertUser(userBean);
//            if (status){
//                System.out.println("注册成功！");
//            }else {
//                System.out.println("注册失败！");
//            }
//        }
//        System.out.println(userBean.getUserName()+".properties"+"文件已存在");
    }

    public boolean login(String userName, Integer password) throws IOException {
        UserBean user = bankDao.findUser(userName, password);
        if (user == null){
            return false;
        }else {
            return true;
        }
//        File file = new File("src/main/resources/"+userName+".properties");
//        if (!file.exists()){
//            System.out.println(userName+".properties"+"文件不存在");
//            return false;
//        }
////        InputStream in = ManagerImpl.class.getResourceAsStream(userName+".properties");
//        InputStream in = ManagerImpl.class.getClassLoader().getResourceAsStream(userName+".properties");
//        if (in == null){
//            System.out.println(userName+".properties"+"查找异常");
//        }
//        Properties properties = new Properties();
//        properties.load(in);
//        String userName1 = properties.getProperty("userName");
//        String password1 = properties.getProperty("password");
//        if (userName1 == null){
//            System.out.println("用户不存在！");
//            return false;
//        }
//        if (userName.equals(userName1) && password.toString().equals(password1)){
//            return true;
//        }
//        return false;
    }

    public boolean transfer(String userName, double money) throws IOException {
        File file = new File("src/main/resources/" + userName + ".properties");
        if (!file.exists()){
            System.out.println("该用户不存在");
        }
        if (money <= 0){
            System.out.println("转账金额不能低于0元");
        }
        double leftMoney = userBean.getMoneyBean().getMoney();
        if (leftMoney < money){
            System.out.println("余额不足，不可转账");
        }
        boolean status1 = bankDao.updateMoney(userBean.getUserName(), leftMoney - money);
        if (status1){
            UserBean toUserBean = bankDao.findByName(userName);
            Double toUserBeanMoney = toUserBean.getMoneyBean().getMoney();
            boolean status2 = bankDao.updateMoney(userName, toUserBeanMoney + money);
        }
        return true;
    }
}
